<?php
namespace Tyrone\Model\Game;
use Tyrone\Model\Base;
use Medoo\Medoo;

class PayLog extends Base
{
    protected $table = 'l_player_pay';


    public function __construct($database,$db_name='')
    {
        parent::__construct($database,$db_name);
        $this->table = singleDb($db_name,$this->table);
    }

    /**
     * 统计充值金额，次数，人数。。。
     * @param array $payMap
     * @return mixed
     */
    public function statPay($payMap = array())
    {
        $res = $this->database->select("{$this->table}(p)",
        [
            '[>]role(r)' => ['p.role_id' => 'id'],
        ],
            [
            'p.platform',
            'rmb' => Medoo::raw('SUM(p.rmb)'),
            'num' => Medoo::raw('COUNT(p.id)'),
            'players' => Medoo::raw('COUNT(DISTINCT p.role_id)'),
        ], $payMap + [
            'GROUP' => 'p.platform'
        ]
        );


        return array_column($res, null, 'platform');
    }

    public function getRoleGroupData($day = null){
        $where = [];
        if($day){
            $where['pay_date'] = $day;
        }
        return $this->database->select($this->table,[
            'rmb' =>Medoo::raw('SUM(rmb)'),
            'role_id',
        ],$where + [
            'GROUP' => 'role_id'
        ]);

    }

    public function getPayDataByDate($date,$is_first=null){
        if($is_first===1){
            $params = [':date' => $date];
            $sql = "SELECT rpl.role_id,rpl.pay_date
FROM role_pay_log rpl
INNER JOIN (SELECT role_id, MIN(pay_time) AS min_pay_time FROM role_pay_log GROUP BY role_id) fp 
ON rpl.role_id = fp.role_id AND rpl.pay_time = fp.min_pay_time
WHERE rpl.pay_date = :date
ORDER BY rpl.pay_time";
            return $this->database->query($sql,$params)->fetchAll();
        }else{
            return $this->database->select($this->table.'(rpl)',[
                'rpl.role_id',
                'rpl.pay_date'
            ],[
                'rpl.pay_date' => $date
            ]);
        }

    }

}